<?php
class LoginModel extends Model{

	protected $_validate = array(
	array('username','require','用户名不可为空！',0,'',3),
	array('password','require','密码不可以为空！',0,'',3),
	);

	/*
	 * 检查登录态
	 */
	public function checkLogin(){
		$user = D("User");
		$username = Cookie::get('username');
		$data = $user->where("username = '$username'")->select();
		if (!empty($username) && !empty($data)){
			$psw = $data[0]["password"];
			$skey = md5($username.$psw);
			if ($skey == Cookie::get('skey')){
				return true;
			}
		}else {
			return false;
		}
	}

	public function loginIn($uname,$psw){
		if (!empty($uname) && !empty($psw)){
			$user = D('User');
			$name = $user->where("username='$uname' AND password='$psw'")->select();
			if (!empty($name)){
				Cookie::set('username',$uname);	//登录成功后，set_cookie,设置有效期为半小时
				Cookie::set('skey', md5($uname.$psw));  //下发skey（用户名+密码取md5值）,有效期为半小时
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}
}